package com.yilian.xlmusic.android.view.adapter;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseDelegateMultiAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.yilian.xlmusic.android.MoreCategoryActivity;
import com.yilian.xlmusic.android.MoreMusicActivity;
import com.yilian.xlmusic.android.R;
import com.yilian.xlmusic.android.utils.RecycleViewPager;
import com.yilian.xlmusic.android.utils.net.AppConstants;
import com.yilian.xlmusic.android.view.bean.MusicPlayerBean;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class FoudMusicAdapter extends BaseDelegateMultiAdapter<MusicPlayerBean, BaseViewHolder> {
    List<MusicPlayerBean> dataList;
    public FoudMusicAdapter() {
        super();
        // 第一步，设置代理
        setMultiTypeDelegate(new BaseMultiTypeDelegate<MusicPlayerBean>() {
            @Override
            public int getItemType(@NotNull List<? extends MusicPlayerBean> data, int position) {
                dataList = (List<MusicPlayerBean>) data;
                if (data.get(position).getItem_type() == MusicPlayerBean.MusicCategory) {
                    return MusicPlayerBean.MusicCategory;
                }
                return MusicPlayerBean.Tuijian;
            }
        });
        // 第二部，绑定 item 类型
        Objects.requireNonNull(getMultiTypeDelegate())
                .addItemType(MusicPlayerBean.MusicCategory, R.layout.music_category_adapter_layout)
                .addItemType(MusicPlayerBean.Tuijian, R.layout.normal_adapter_layout);

    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder,MusicPlayerBean musicBean) {
        baseViewHolder.setText(R.id.type_title,musicBean.gettitle());

        if (musicBean.getItem_type() == MusicPlayerBean.MusicCategory) {//音乐分类
            RecyclerView music_category_view = baseViewHolder.itemView.findViewById(R.id.music_category_view);
            music_category_view.setLayoutManager(new GridLayoutManager(getContext(), 2));
            MCategoryAdapter mCategoryAdapter = new MCategoryAdapter();
            mCategoryAdapter.setList(musicBean.getCategory());
            music_category_view.setAdapter(mCategoryAdapter);
            baseViewHolder.getView(R.id.more_category_btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //更新适配器的内容
                    if(AppConstants.changeItems.size()>0){
                        AppConstants.refreshView(AppConstants.changeItems.get(0));
                    }
                    Intent intent = new Intent(getContext(), MoreCategoryActivity.class);
                    getContext().startActivity(intent);
                }
            });
            mCategoryAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    mCategoryAdapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                            //更新适配器的内容
                            if(AppConstants.changeItems.size()>0){
                                AppConstants.refreshView(AppConstants.changeItems.get(0));
                            }
                            Intent intent = new Intent(getContext(), MoreMusicActivity.class);
                            intent.putExtra("category_id", musicBean.getCategory().get(position).getId());
                            getContext().startActivity(intent);
                        }
                    });
                }
            });
        } else {
            RecycleViewPager viewPager = baseViewHolder.itemView.findViewById(R.id.itenViewPager);
            int position = baseViewHolder.getAdapterPosition();
            viewPager.setId((position+1));

            List<String> strings = new ArrayList<>();
            for (int i = 0; i < musicBean.getDataBean().size()/3; i++) {
                strings.add("page"+i);
            }
            baseViewHolder.getView(R.id.more_btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //更新适配器的内容
                    if(AppConstants.changeItems.size()>0){
                        AppConstants.refreshView(AppConstants.changeItems.get(0));
                    }
                    Intent intent = new Intent(getContext(), MoreMusicActivity.class);
                    if(musicBean.gettitle().equals("热门推荐")){
                        intent.putExtra("category_id", -1);
                    }else{
                        intent.putExtra("category_id", musicBean.getId());

                    }
                    getContext().startActivity(intent);
                }
            });

            MusicViewPagerAdapter musicViewPagerAdapter = new MusicViewPagerAdapter(getContext(), strings,musicBean.getDataBean() ,viewPager,this);
            viewPager.setAdapter(musicViewPagerAdapter);
        }
    }


}
